home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Apple Script / scripts / ConvtDt Folder / Date Conversion Samples next >
Text File  |  1994-03-21  |  3KB  |  57 lines

  1. -- this library is SHAREWARE.  If you keep it past the 10 day tryout period, you are
  2. -- REQUIRED to register it.  If you decide not to keep it, please dispose of it.
  3.  
  4. -- to register your copy, mail $5.00 U.S. to:
  5. -- Allan Crump
  6. -- P.O. Box 26322
  7. -- San Francisco, California 94111
  8. --     (in return I'll send you the latest registered copy of Date Conversions as well
  9. --       as some other useful methods of incorporating Julian dates into routines and programs
  10.  
  11.  
  12. property DateLib : "null" -- use this if you want to use the 'property'
  13.  
  14. -- a fine place to put the library is in your System:Extensions:Scripting additions so that
  15. -- you can load them dynamically as follows (this code works on any machine)
  16.  
  17. set pExt to (path to extensions as string) & "Scripting Additions:Date Conversions" as string
  18. if not class of DateLib = script then
  19.     set DateLib to load script (alias pExt)
  20. end if
  21.  
  22. --first, we'll tell datelib to convert the date "11/18/93 1:45:39 AM" to a full Julian number
  23. -- this is great for calculating the difference between two times in a day or
  24. -- two dates with mixed times.  The decimal return of the number is the portion
  25. -- of the day in hundredths.
  26.  
  27. tell DateLib to copy FullDateToJulian(date "Thursday, February 18, 1993 1:45:39 AM") to x
  28. display dialog (x as text)
  29.  
  30.  
  31. --then, we'll take that number and convert it back to the date format.
  32. --notice in some cases that there is a one-second variance in the return. This is due
  33. -- to the mathematics of the formula.
  34.  
  35. tell DateLib to copy JulianToFullDate(x) to MyDate
  36. display dialog (MyDate as text)
  37.  
  38. -- Now, let's say that we need to figure out the difference between just two dates.
  39. -- instruct Datelib to copy the DayToJulian of the date (like 11/17/93) to an integer Julian #
  40.  
  41. tell DateLib to copy DayToJulian(date "Wednesday, November 17, 1993 12:00:00 AM") to x
  42. display dialog (x as text)
  43.  
  44. -- Then we'll convert it back after doing mathematics on it and comparing it to a new number
  45. -- this is done with the JulianToDay statement as shown:
  46. tell DateLib to copy JulianToDay(x) to MyDate
  47. display dialog (MyDate as text)
  48.  
  49. -- for example, you could take one date, say 09/14/88 and subtract 
  50. -- another birthday like say, 08/02/86 and tell how many days were between them.
  51.  
  52. tell DateLib to copy DayToJulian(date "Wednesday, September 14, 1988 12:00:00 AM") - DayToJulian(date "Saturday, August 2, 1986 12:00:00 AM") to diff
  53. display dialog (("There are " & diff as text) & " days between the two dates.")
  54.  
  55. -- Have Fun !  Remember, if you want to use this function in a routine, you'll be bothered
  56. -- with the nasty "Register" message occasionally, until it's registered.
  57. -- Allan